package org.rcgwt.client.ui.grid.renderer;

import org.rcgwt.client.ui.grid.internal.model.IGroupTitleModel;
import org.rcgwt.client.ui.grid.theme.CssStyle;

import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.TableCellElement;
import com.google.gwt.dom.client.TableRowElement;

/**
 * 
 * @author Olivier Oeuillot (latest modification by $Author: $)
 * @version $Revision: $ $Date: $
 */
public class DefaultGroupTitleRenderer extends
        AbstractRenderer<IGroupTitleModel> implements IGroupTitleRenderer {

    @Override
    public Element render(IViewRenderContext viewRenderContext,
            Element parentElement, IGroupTitleModel group, int renderedIndex) {

        Document document = parentElement.getOwnerDocument();

        TableRowElement tableRow = document.createTRElement();

        TableCellElement tableCell = document.createTDElement();
        tableCell.setColSpan(viewRenderContext.getGridRenderContext()
                .getGridModel().listColumnModels().length);
        tableRow.appendChild(tableCell);

        update(viewRenderContext, tableRow, group, renderedIndex);

        return tableRow;
    }

    @Override
    public void update(IViewRenderContext viewRenderContext,
            Element groupTitleElement, IGroupTitleModel groupTitleModel,
            int renderedIndex) {
        super.update(viewRenderContext, groupTitleElement, groupTitleModel,
                renderedIndex);

        viewRenderContext.getViewModel().getViewModelElements()
                .setGroupTitleElement(groupTitleModel, groupTitleElement);
    }

    @Override
    protected void updateContent(IViewRenderContext viewRenderContext,
            Element groupTitleElement, IGroupTitleModel groupTitleModel) {

        TableCellElement cellElement = TableCellElement.as(TableRowElement.as(
                groupTitleElement).getFirstChildElement());

        updateCellContent(viewRenderContext, cellElement, groupTitleModel);
    }

    protected void updateCellContent(IViewRenderContext viewRenderContext,
            TableCellElement cellElement, IGroupTitleModel groupTitleModel) {

        String name = getGroupName(viewRenderContext, groupTitleModel);

        if (name == null) {
            name = "Group " + groupTitleModel.getGroupModel().getModelIndex();
        }

        cellElement.setInnerText(name);
    }

    protected String getGroupName(IViewRenderContext viewRenderContext,
            IGroupTitleModel groupTitleModel) {

        String name = viewRenderContext.getGridRenderContext().getGridModel()
                .getGridWidget().getGroupDataModel().getGroupName(
                        groupTitleModel.getGroupModel().getGroup());

        return name;
    }

    @Override
    protected CssStyle getCssStyle(IViewRenderContext viewRenderContext,
            IGroupTitleModel modelElement) {
        return viewRenderContext.getGridRenderContext().getTheme()
                .getGroupTitleCssStyle(modelElement);
    }
}
